//
//  ANUserPasswordViewController.m
//  Ants
//
//  Created by ZHM on 16/8/6.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANUserPasswordViewController.h"
#import "ANUserHeaderViewController.h"

#define ANLoginOKNote @"loginOKNote"

@interface ANUserPasswordViewController ()
{
    UIView *bgView;
    UIView *bgViewRepeat;
    UITextField *passward;
    UITextField *repeatPassward;
}
@end

@implementation ANUserPasswordViewController

- (instancetype)init {
    if (self = [super init]){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"注册2/3";
    self.view.backgroundColor=[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    
    [self createTextFields];
}

-(void)createTextFields
{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(30, 75, self.view.frame.size.width-90, 30)];
    label.text = [NSString stringWithFormat:@"请设置密码 - %@",self.userPhoneNum];
    label.textColor=[UIColor grayColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    CGRect frame=[UIScreen mainScreen].bounds;
    bgView=[[UIView alloc]initWithFrame:CGRectMake(10, 110, frame.size.width-20, 46)];
    bgView.layer.cornerRadius = 4.0;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    
    bgViewRepeat = [[UIView alloc]initWithFrame:CGRectMake(10, 170, frame.size.width-20, 46)];
    bgViewRepeat.layer.cornerRadius = 4.0;
    bgViewRepeat.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgViewRepeat];
    
    passward=[self createTextFielfFrame:CGRectMake(100, (bgView.height - 30)/2, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"6-20位字母或数字"];
    passward.clearButtonMode = UITextFieldViewModeWhileEditing;
    passward.secureTextEntry=YES;
    
    repeatPassward=[self createTextFielfFrame:CGRectMake(100, (bgViewRepeat.height - 30)/2, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"6-20位字母或数字"];
    repeatPassward.clearButtonMode = UITextFieldViewModeWhileEditing;
    repeatPassward.secureTextEntry=YES;
    
    UILabel *phonelabel=[[UILabel alloc]initWithFrame:CGRectMake(20, (bgView.height - 25)/2, 60, 25)];
    phonelabel.text=@"密码";
    phonelabel.textColor=[UIColor blackColor];
    phonelabel.textAlignment=NSTextAlignmentLeft;
    phonelabel.font=[UIFont systemFontOfSize:14];
    
    UILabel *repeatPhonelabel=[[UILabel alloc]initWithFrame:CGRectMake(20, (bgViewRepeat.height - 25)/2, 60, 25)];
    repeatPhonelabel.text=@"确认密码";
    repeatPhonelabel.textColor=[UIColor blackColor];
    repeatPhonelabel.textAlignment=NSTextAlignmentLeft;
    repeatPhonelabel.font=[UIFont systemFontOfSize:14];
    
    
    UIButton *landBtn=[self createButtonFrame:CGRectMake(10, CGRectGetMaxY(bgViewRepeat.frame) + 20,self.view.frame.size.width-20, 37) backImageName:nil title:@"下一步" titleColor:[UIColor whiteColor]  font:[UIFont systemFontOfSize:17] target:self action:@selector(landClick)];
    landBtn.backgroundColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    landBtn.layer.cornerRadius=5.0f;
    
    [bgView addSubview:passward];
    [bgViewRepeat addSubview:repeatPassward];
    [bgView addSubview:phonelabel];
    [bgViewRepeat addSubview:repeatPhonelabel];
    [self.view addSubview:landBtn];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [passward resignFirstResponder];
    [repeatPassward resignFirstResponder];
}
-(void)landClick
{
//    if([passward.text isEqualToString:@""] && [repeatPassward.text isEqualToString:@""])
//    {
//        [SVProgressHUD showInfoWithStatus:@"您还未设置密码"];
//        return;
//    }else if (passward.text.length <6 && repeatPassward.text.length <6)
//    {
//        [SVProgressHUD showInfoWithStatus:@"亲,密码长度至少六位"];
//        return;
//    }else if (![passward.text isEqualToString:repeatPassward.text]) {
//        [SVProgressHUD showInfoWithStatus:@"两次密码不同!"];
//        return;
//    }
//    
//    [BmobUser signOrLoginInbackgroundWithMobilePhoneNumber:self.userPhoneNum SMSCode:self.code andPassword:passward.text block:^(BmobUser *user, NSError *error) {
//        if (user) {
//            ANUserHeaderViewController *vc = [[ANUserHeaderViewController alloc]init];
//            vc.user = user;
//            [[NSNotificationCenter defaultCenter] postNotificationName:ANLoginOKNote object:nil];
//            [sharedWebService fetchUserBasicInfoWithCompletion:nil];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//    }];
    
    //测试直接跳过
    ANUserHeaderViewController *vc = [[ANUserHeaderViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField=[[UITextField alloc]initWithFrame:frame];
    
    textField.font=font;
    
    textField.textColor=[UIColor grayColor];
    
    textField.borderStyle=UITextBorderStyleNone;
    
    textField.placeholder=placeholder;
    
    return textField;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor=color;
    }
    
    return imageView;
}

-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

@end
